package com.yinhai.contacts.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.yinhai.activity.LocalContactJoinActivity;
import com.yinhai.activity.R;
import com.yinhai.contacts.bean.SameContactAda;

public class ContactsSameListAdapter extends BaseAdapter {
	//private Context ctx;
	private List<SameContactAda> list;
	private LayoutInflater inflater;
	Context context;
	SameContactAda selectCn;
	private AQuery aq = null;
	public ContactsSameListAdapter(Context context, List<SameContactAda> list) {
		this.context = context;
		this.list = list;
		this.inflater = LayoutInflater.from(context);
	}
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		convertView = inflater.inflate(R.layout.samecontact_list_item, null);
		holder = new ViewHolder();
		holder.listdata = (ListView) convertView.findViewById(R.id.sublistview);
		holder.join = (ImageView) convertView.findViewById(R.id.join);
		holder.layout_id = (LinearLayout) convertView.findViewById(R.id.layout_id);
		convertView.setTag(holder);
		aq = new AQuery(convertView);
		selectCn = list.get(position);
	/*	holder.join.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				
			}
		});*/
		
		aq.id(R.id.join).tag(selectCn).clicked(onclick);
		
		
		for(int i = 0 ;i<selectCn.getSameRecordList().size(); i++ ){
			View v = inflater.inflate(R.layout.samecontact_list_subitem, null);
			TextView tv = (TextView) v.findViewById(R.id.dial_item_name);
			tv.setText(selectCn.getSameRecordList().get(i).getSameItem());
			TextView tvtel = (TextView) v.findViewById(R.id.dial_item_tel);
			tvtel.setText(selectCn.getSameRecordList().get(i).getMomeItem());
			holder.layout_id.addView(v);
		}
		/*ContactsSameListSubAdapter csAdatpter = new ContactsSameListSubAdapter(context,selectCn.getSameRecordList());
		holder.listdata.setAdapter(csAdatpter);*/
		
		return convertView;
	}

	public static class ViewHolder {
		ListView listdata;
		ImageView join;
		LinearLayout layout_id ;
	}

	OnClickListener onclick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			SameContactAda sca = (SameContactAda)v.getTag();
			Intent mIntent = new Intent(context,LocalContactJoinActivity.class);
			Bundle mBundle = new Bundle();
			mBundle.putString("flag", sca.getFlag());
			mBundle.putString("main_contacts_name", sca.getMain_contacts_name());
			String contacts_rawId_list = sca.getMain_contacts_rawId()+",";
			for(String rawId:sca.getContacts_rawId_list()) {
				contacts_rawId_list=contacts_rawId_list+rawId+",";
			}
			mBundle.putString("contacts_rawId_list", contacts_rawId_list);
			String contacts_phone_list = "";
			for(String rawId:sca.getContacts_phone_list()) {
				contacts_phone_list=contacts_phone_list+rawId+",";
			}
			mBundle.putString("contacts_phone_list",contacts_phone_list);
			
//	        mBundle.putSerializable(ContactsUtils.CONTACT_KEY, sca);
	        mIntent.putExtras(mBundle); 
			context.startActivity(mIntent);
		}
	};
	
}
